home *** CD-ROM | disk | FTP | other *** search
- Path: indirect.com!pronet01
- From: pronet01@indirect.com (Mark Miller)
- Newsgroups: comp.lang.c++
- Subject: gnu problem with ternary & references
- Date: Sat, 3 Feb 1996 19:12:17 UNDEFINED
- Organization: Promark One
- Message-ID: <pronet01.26.001ACCB0@indirect.com>
- NNTP-Posting-Host: s29.phxslip4.indirect.com
- X-Newsreader: Trumpet for Windows [Version 1.0 Rev B final beta #1]
-
- Gang,
-
- I wanted to share something with you that hopefully will save you quite
- a few hours of debugging if you ever run across this scenario...
-
- Buried deep into one of my class libraries, I had a method returning
- a const reference (for efficiency reasons rather than a copy) to an attribute.
- I knew I had a value in the attribute I was trying to get, but was ALWAYS
- getting back an empty string. I was trying to return a reference to "" rather
- than to an static object (that was a string object set to "")..
-
- Here's what I saw with Gnu (and can reproduce it everytime) and I'm still
- trying to see if this is a compiler bug or a "feature"..
-
- If you have a statement like (using C++ Views by Liant):
-
- VString & someClass::getString(const int &i)
- {
- VString *pString = (VString *)StringCollect.idAt(i);
-
- return pString != (VString *)NULL ? *pString : "";
- }
-
- Whether pString is NULL or not, you will ALWAYS get back an empty reference.
- This is what blew my mind...
-
- IF you replace the:
-
- return pString != (VString *)NULL ? *pString : "";
- with:
-
- if (pString != (VString *)NULL)
- return *pString;
- else
- return "";
-
- it WILL work...
-
- HOWEVER, the following ALSO WORKS:
-
- static VString emptyRef;
- ...
-
- return pString != (VString *)NULL ? *pString : emptyRef;
-
- So, for some reason (I'm hoping GNU was trying to inform me that passing
- a reference to "" (by accident) is evil, OR there's a flat out bug in the GNU
- C++ compiler. Trying the same code with Borland or Microsofts compiler
- works with all 3 ways...
-
- On the subject of gnu c++ compiler problems includes the other scenario
- where the compiler belches when you try and do:
-
- switch (value) {
- case ONE:
- int x; // Compiler will belch here
- ...
- break;
- }
- and requires you to do:
-
- if (value == ONE) {
- int x; // Compiler will NOT belch here
- ...
- }
-
- This may be a dumb question, but in my 3 years of C++ programming, I never
- could find the answer to this in any of my 6 books concerning references...
-
- I believe that it is evil to return a reference to "" rather than to object x
- (which is set to "").. If it is NOT evil (if the compiler does something
- similar to what 'delete NULL' does), please let me know if you know the
- answer.. Please email me directly as I rarely ever logon to the net...
-
- thanks,
- theron kousek
- promark 1
- phoenix, AZ
-
-
-
-